//
//  SRVCVConnectProbe.m
//  iPhoneSRV
//
//  Created by Nicholas Kitchener on 23/05/2010.
//  Copyright 2010 None. All rights reserved.
//

#import "SRVUICVConnectProbe.h"

@implementation SRVUICVCConnectProbe
@synthesize ipConnected, bfPresent, bfVersionString;

-(id)init:(SRVBlackfinController*)target {
	if( self==[super init] ) {
		ipConnected=NO;
		bfPresent=NO;
		bfVersionString = @"Failed IP connect";
		blackfin = target;
		
		connect = [SRVConnectOperation connectWith:blackfin];
		[connect setOperationTimeout:2.0];
		[connect setCallback:self];
		[blackfin queueOperation:connect];
	}
	return self;
}

-(void)operationCompleted:(id)operation {
	if( ([(SRVNSOperation*)operation complete] == YES)
	    && ([(SRVNSOperation*)operation expired] == NO) ) {
		if(ipConnected==NO) {
			bfVersionString = @"No Blackfin detected";
			ipConnected = YES;
			[((SRVConnectOperation*) operation) release];
			version = [[SRVGetVersionOperation alloc] init];
			[version setOperationTimeout:2.0];
			[version setCallback:self];
			[blackfin queueOperation:version];		
		} else {
			bfPresent = YES;
			bfVersionString = [[version versionString]retain]; // causes leak of not released
			[((SRVGetVersionOperation*) operation) release];
		}
	} else {
		NSLog(@"%i: Connection Probe - %@ expired", [blackfin portNumber], operation);
		if(ipConnected) {
			[blackfin closeConnection:nil];
			ipConnected = NO;
		}
	}
	
}
@end

